package com.zx.cloud.security;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.zx.cloud.common.utils.JsonUtil;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.xml.crypto.Data;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * @author zhaoxuan
 * @date 2022-04-25 09:56
 **/
public class ExcelMain {
    public static Set<String> codeSet=new HashSet<>();
    public static Set<String> regCodeSet=new HashSet<>();
    public static Set<String> codeSet1=new HashSet<>();
    public static Set<String> regCodeSet1=new HashSet<>();
    public static void main(String[] args) throws InterruptedException {
        /*DateTimeFormatter DATE_TIME_FORMATTER =DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.s");
        DateTimeFormatter DATE_TIME_FORMATTER1 =DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        List<Map> list = ExcelImportUtil.importExcel(new File("D:\\工作文档\\诚信通\\20221228_测试.xlsx"), Map.class, params);
        StringBuffer sql=new StringBuffer();
        sql.append("insert into WORKDAYS (WorkDay_Date,WorkDay_Type,WorkDay_OnTime,WorkDay_OffTime) \n");
        list.forEach(x->{
            LocalDateTime workdate=LocalDateTime.parse((String)x.get("workdate"),DATE_TIME_FORMATTER);
            String workdateStr=workdate.format(DATE_TIME_FORMATTER1);
            LocalDateTime onTime = workdate.plusHours(8).plusMinutes(30);
            String onTimeStr=onTime.format(DATE_TIME_FORMATTER1);
            LocalDateTime offTime = workdate.plusHours(17).plusMinutes(30);
            String offTimeStr=offTime.format(DATE_TIME_FORMATTER1);
            String workday = (String) x.get("workday");
            String values="('"+workdateStr+"','"+workday+"','"+onTimeStr+"','"+offTimeStr+"'),\n";
            sql.append(values);
        });

        System.out.println(sql.toString());*/
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        Set<String> snoSet=new HashSet<>();
        List<Map> list1 = ExcelImportUtil.importExcel(new File("D:\\工作文档\\信用监控\\年报数据处理\\1w4年报处理结果\\信用监控-企查查2021-2000-3000家.xlsx"), Map.class, params);
        List<Map> list2 = ExcelImportUtil.importExcel(new File("D:\\工作文档\\信用监控\\年报数据处理\\1w4年报处理结果\\信用监控-企查查2021-3000-4000家.xlsx"), Map.class, params);
        List<Map> list3 = ExcelImportUtil.importExcel(new File("D:\\工作文档\\信用监控\\年报数据处理\\1w4年报处理结果\\信用监控-企查查2021-4001-9000家.xlsx"), Map.class, params);
        List<Map> list4 = ExcelImportUtil.importExcel(new File("D:\\工作文档\\信用监控\\年报数据处理\\1w4年报处理结果\\信用监控-企查查2021-9001-14000家.xlsx"), Map.class, params);
        List<Map> list5 = ExcelImportUtil.importExcel(new File("D:\\工作文档\\信用监控\\年报数据处理\\1w4年报处理结果\\信用监控-企查查2021-14001-14298家.xlsx"), Map.class, params);
        List<Map> list6 = ExcelImportUtil.importExcel(new File("D:\\工作文档\\信用监控\\年报数据处理\\1w4年报处理结果\\信用监控-企查查2021-前2000.xlsx"), Map.class, params);
        list1.forEach(x->{
            snoSet.add((String)x.get("sno"));
        });
        list2.forEach(x->{
            snoSet.add((String)x.get("sno"));
        });
        list3.forEach(x->{
            snoSet.add((String)x.get("sno"));
        });
        list4.forEach(x->{
            snoSet.add((String)x.get("sno"));
        });
        list5.forEach(x->{
            snoSet.add((String)x.get("sno"));
        });
        list6.forEach(x->{
            snoSet.add((String)x.get("sno"));
        });
        System.out.println(snoSet.size());

        List<Test2Main.Basic> collect = snoSet.stream().map(x -> {
            Test2Main.Basic basic = new Test2Main.Basic();
            basic.setSno(x);
            return basic;
        }).collect(Collectors.toList());
        ExportParams exportParams = new ExportParams();
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Test2Main.Basic.class,collect);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("D:\\工作文档\\信用监控\\年报数据处理\\1w4年报处理结果\\4470家sno.xlsx");
            workbook.write(fos);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (fos!=null){
                try {
                    fos.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
/*        Map<String, Object> hashMap = new HashMap<>();
        List<Map<String,Object>> list=new ArrayList<>();
        List<Map<String,Object>> list2=new ArrayList<>();
        for(int i=1;i<=10;i++){
            Map<String, Object> map = new HashMap<>();
            map.put("num",i);
            map.put("name","zx"+i);
            list.add(map);
            list2.add(map);
        }
        hashMap.put("list",list);
        hashMap.put("list2",list2);
        List<Map<String,Object>> list3=new ArrayList<>();
        list3.add(hashMap);
        ExportParams params = new ExportParams();
        Workbook workbook = ExcelExportUtil.exportExcel(params,);

        File file = new File("D:\\text.xlsx");
        try {
            FileOutputStream outputStream = new FileOutputStream(file);
            workbook.write(outputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }*/

/*
        TransmittableThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();
        threadLocal.set(Thread.currentThread().getName());
        System.out.println(Thread.currentThread().getName()+":"+threadLocal.get());
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+":"+threadLocal.get());
            //threadLocal.set(Thread.currentThread().getName());
            System.out.println(Thread.currentThread().getName()+":"+threadLocal.get());
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+":"+threadLocal.get());
            }).start();
        }).start();
        Thread.sleep(2000);

        System.out.println(Thread.currentThread().getName()+":"+threadLocal.get());
*/

/*        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        List<Map> list = ExcelImportUtil.importExcel(new File("D:\\工作文档\\信用监控\\ccx登记机关地址映射新20221018.xlsx"), Map.class, params);
        StringBuffer sb=new StringBuffer();
        sb.append("INSERT INTO monitor_org(org,province,city,addressid) \nvalues\n");
        list.forEach(x->{
            sb.append("('").append(x.get("登记机关")).append("','").append(x.get("省份")).append("','").append(x.get("城市")).append("','606'),\n");
        });
        String sql = sb.replace(sb.length() - 2, sb.length() - 1, ";").toString();
        System.out.println(sql);*/
        /*

        List<Map> list = ExcelImportUtil.importExcel(new File("D:\\工作文档\\信用监控\\1w加企业.xlsx"), Map.class, params);
        list.forEach(x -> {
            String code = (String) x.get("统一社会信用代码");
            String regCode = (String) x.get("注册号");
            if (StringUtils.isNotEmpty(code)){
                codeSet.add(code);
            }
            if (StringUtils.isNotEmpty(regCode)){
                regCodeSet.add(regCode);
            }
        });
        codeSet.stream().limit(1).forEach(x->{
                    alSendInfo(x,"2");
                }
        );*/
/*        System.out.println("12号："+countExist(new File("D:\\工作文档\\信用监控\\20220612变更.xlsx")));
        System.out.println("13号："+countExist(new File("D:\\工作文档\\信用监控\\20220613变更.xlsx")));
        System.out.println("14号："+countExist(new File("D:\\工作文档\\信用监控\\20220614变更.xlsx")));
        System.out.println("15号："+countExist(new File("D:\\工作文档\\信用监控\\20220615变更.xlsx")));
        int count=regCodeSet1.size()+codeSet1.size();
        System.out.println("4天数据:"+count);*/
    }
    public static void alSendInfo(String key,String type){
        String result= HttpUtil.get("http://localhost:8182/enterprise/getEntBaseInfo?key="+key+"&type="+type);
        JSONObject jsonObject = JSONUtil.parseObj(result);

        Map<String,Object> datamap=(Map<String,Object>) jsonObject.getJSONObject("data").get("BASIC");
        HashMap<String, Object> hashMap = new HashMap<>();
        datamap.forEach((k,v)->{

        });
        System.out.println();


    }
    public static int countExist(File file){
        //AtomicInteger count=new AtomicInteger(0);
        ImportParams params = new ImportParams();
        Set<String> set1=new HashSet<>();
        Set<String> set2=new HashSet<>();
        params.setHeadRows(1);
        List<Map> list = ExcelImportUtil.importExcel(file, Map.class, params);
        list.stream().forEach(x->{
            String code = (String) x.get("code");
            String regCode = (String) x.get("regcode");
            if (StringUtils.isNotEmpty(code)){
                if (codeSet.contains(code)){
                    set1.add(code);
                    codeSet1.add(code);
                }
            }else {
                if (regCodeSet.contains(regCode)){
                    set2.add(regCode);
                    regCodeSet1.add(regCode);
                }
            }
        });

        return set1.size()+set2.size();
    }
}
